package tix

// -------------------------------------------------------------------------------------------------

object Tix extends App {
  util.Properties.setProp("scala.time", "true")
  delayedInit(run)

  /** Force loading the package objects. */
  List(GMP, MPFR, MPC, GCC, GLibc, GTest, GLog, GFlags)

  /** Formats an array of bytes as a single-line string. */
  def format(bytes: Array[Byte]): String = bytes.map { "%02x" format _ } mkString ","

  /** Formats an array of bytes as a multi-line string. */
  def format(bytes: Array[Byte], split: Int, sep: String = "\n"): String = {
    if (bytes.length <= split)
      format(bytes)
    else {
      val (a1, a2) = bytes.splitAt(split)
      List(format(a1), format(a2, split, sep)).mkString(sep)
    }
  }

  /** Installs the package specified by its name. */
  def install(pkgname: String) {
    val pkg: Package = PackageUniverse.packages(pkgname)
    println(pkg)
    for ((version, factory) <- pkg.factories) {
      println("Installing version " + version + " through factory " + factory)
      val config = new Config(Map())
      val pkgVersion: itf.PackageVersion = factory(config)
      println(pkgVersion)
      val wf = pkgVersion.workflow()
      wf.install()
    }
  }

  def run {
    Console.println("Start")
    for ((name, pkg) <- PackageUniverse.packages) {
      Console.println(name + ": " + pkg)
    }

    if (false) {
      val v = new GMP.V5_0_2(Map('config -> None))
      println(v)
      println(Hasher.md5(v))

      val bytes = Serializer.serialize(v)
      println(format(bytes, split = 16))

      val v2 = Serializer.deserializeAs[GMP.V5_0_2](bytes)
      println(v2)
      println(Hasher.md5(v2))
    }

    install("GMP")
  }
}